<?php

namespace App\Http\Controllers;

use App\Models\Cart;
use App\Models\Product;
use App\Models\ProductType;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends Controller
{
    /**
     * 列表
     * GET /api/product
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request): JsonResponse
    {
        $params = $request->all();

        $productType = new ProductType();
        $params['status'] = 'ENABLE';
        $typeList = $productType->getList($params);

        // 产品列表
        $product = new Product();
        $params['status'] = 'ENABLE';
        $productList = $product->getList($params);

        // 已登陆
        if (!empty($params['customer_id']) && !empty($params['store_id'])) {
            // 查询加购数量
            $carts = DB::table('cart')
                ->where('customer_id', '=', $params['customer_id'])
                ->where('store_id', '=', $params['store_id'])
                ->get()
                ->map(function ($value) {
                    return (array)$value;
                })->toArray();
            $cartProduct = array_column($carts, 'count', 'product_id');
            foreach ($productList as $item) {
                $item->cart_count = $cartProduct[$item->id] ?? 0;
            }
        }

        $data = [];
        $start = 0;
        foreach ($typeList as $item) {
            $tmp['id'] = $start;
            $tmp['name'] = $item->name;
            $plist = [];
            foreach ($productList as $value) {
                if ($value->type_id == $item->id) {
                    $plist[] = $value;
                }
            }
            $tmp['subArr'] = $plist;
            $data[] = $tmp;
            $start++;
        }

        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 获取产品卡路里
     * GET /api/product/kcal
     * @param Request $request
     * @return JsonResponse
     */
    public function kcal(Request $request): JsonResponse
    {
        $params = $request->all();

        $kcal = [
            'protein' => 0,
            'carbon_water' => 0,
            'meals' => 0,
            'salt' => 0,
            'fat' => 0,
            'diversity' => 0,
            'kcal' => 0,
        ];
        if (!empty($params['customer_id']) && !empty($params['store_id'])) {
            $cart = new Cart();
            $carts = $cart->getList($params);

            if (!empty($carts)) {
                foreach ($carts as $item) {
                    $kcal['protein'] += round($item->protein * $item->count, 2);
                    $kcal['carbon_water'] += round($item->carbon_water * $item->count, 2);
                    $kcal['meals'] += round($item->meals * $item->count, 2);
                    $kcal['salt'] += round($item->salt * $item->count, 2);
                    $kcal['fat'] += round($item->fat * $item->count,2);
                    $kcal['diversity'] += round($item->diversity * $item->count, 2);
                    $kcal['kcal'] += round($item->kcal * $item->count,2);
                }
            }
        }
        return response()->json(['status' => 1, 'data' => $kcal]);
    }

    /**
     * 看见卡路里
     * GET /api/product/kcal-product
     * @param Request $request
     * @return JsonResponse
     */
    public function kcalProduct(Request $request): JsonResponse
    {
        $params = $request->all();

        // 产品列表
        $recharge = new Product();
        $params['status'] = 'ENABLE';
        $params['nutrition_flg'] = 1;
        $data = $recharge->getList($params);

        return response()->json(['status' => 1, 'data' => $data]);
    }
}
